首先要使用LinqKit時必須先到NuGet中將LinqKit安裝至參考當中,之後要使用Linq與LinqKit必須先在Controller當中using System.Linq與using LinqKit
下方的Code範例為自訂查詢的LinqKit轉換為Linq的Where篩選查詢
var predicate = PredicateBuilder.New<CAD_STRUCTURE>();
pred = pred.Or(o => o.SOURCE_ID == "123");
pred = pred.Or(o => o.RELATED_ID == "XYZ");
cad.CAD_STRUCTURE.AsExpandable().Where(pred);
//等同於
cad.CAD_STRUCTURE.Where(o => o.SOURCE_ID == "123" || o.RELATED_ID == "XYZ")
var predicate = PredicateBuilder.New<CAD_STRUCTURE>();
pred = pred.And(o => o.SOURCE_ID == "123");
pred = pred.And(o => o.RELATED_ID == "XYZ");
cad.CAD_STRUCTURE.AsExpandable().Where(pred);
//等同於
cad.CAD_STRUCTURE.Where(o => o.SOURCE_ID == "123" && o.RELATED_ID == "XYZ")
var predicate = PredicateBuilder.New<CAD_STRUCTURE>();
for (int i = 0; i <= Select_Level_ParentList.Count() - 1; i++)
{
if (!String.IsNullOrEmpty(Select_Level_ParentList[i]))
{
string Select_Level_Son = Select_Level_SonList[i];
string Select_Level_Parent = Select_Level_ParentList[i];
predicate = predicate.Or(o => o.RELATED_ID == Select_Level_Son &&
o.SOURCE_ID == Select_Level_Parent);
}
else
{
ErrorNullModel NULL = new ErrorNullModel();
ParentViewModelPagedList nullmodel = NULL.NullModel();
int currentPages = pages < 1 ? 1 : pages;
var results = nullmodel.ParentModel.ToPagedList(currentPages, pageSize);
Response.Write("<script language=javascript>alert('父階圖號不可為空');
</"+ "script>");
return View(results);
}
}
var where_CAD_STRUCTURE = db.CAD_STRUCTURE
.Where(predicate);